<template>
  <div>
    <el-dialog title="回收打款" :visible="dialogVisible" :close-on-click-modal="false" @close="close" width="800px">
      <el-descriptions title="订单信息" :column="2" border :labelStyle="{minWidth:'80px',width:'80px',textAlign:'right'}" :contentStyle="{minWidth:'100px',textAlign:'left'}">
        <el-descriptions-item label="申请时间" :span="2">{{orderInfo.createTime}}</el-descriptions-item>
        <el-descriptions-item label="收款账户">{{orderInfo.details}}</el-descriptions-item>
        <el-descriptions-item label="对方名称">{{orderInfo.jiaoyiName}}</el-descriptions-item>

        <el-descriptions-item label="订单类型">
          <span>{{ commonMaps.tenantBillOrderTypeMap[orderInfo.orderType] }}</span>
        </el-descriptions-item>
        <el-descriptions-item label="关联单号">{{orderInfo.orderNo}}</el-descriptions-item>

        <el-descriptions-item label="应付金额" :span="2">
          <span style="color:red;font-size:16px;font-weight:bold">¥{{orderInfo.amount}}</span>
        </el-descriptions-item>
        <el-descriptions-item label="申请备注" :span="2">{{orderInfo.sqRemark}}</el-descriptions-item>
      </el-descriptions>
      <div style="height:20px"></div>

      <el-form :model="ruleForm" ref="ruleForm" :inline="true" label-width="80px" :rules="rules" class="demo-ruleForm">
        <el-form-item label="备注:" prop="remark">
          <el-input style="width:500px" type="textarea" v-model="ruleForm.remark" rows="3" placeholder="请输入备注"></el-input>
        </el-form-item>
        <el-form-item label="打款凭证">
          <el-upload :action="uploadImgUrl" :file-list="imgFileList" list-type="picture-card" :on-preview="handlePictureCardPreview" :on-exceed="uploadNumber" :multiple="true" :drag="true" :on-success="handleUploadSuccess" :limit="5" :on-remove="handleRemove">
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div class="center">
        <el-button @click="close('ruleForm')">取消</el-button>
        <el-button type="danger" v-if="checkPermi(['billList:refuse'])" @click="submitForm('ruleForm',2)" :loading="loading">拒绝打款</el-button>
        <el-button type="primary" v-if="checkPermi(['billList:agree'])" @click="submitForm('ruleForm',1)" :loading="loading">确认打款</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import payApi from "@/api/financeCenter/pay.js";
export default {
  name: "editPrice",
  data() {
    var priceRule = (rule, value, callback) => {
      if (value === "") {
        callback();
      } else {
        if (!this.priceReg.test(value)) {
          callback(new Error("请输入正确的价格"));
        }
        callback();
      }
    };
    return {
      dialogVisible: false,
      loading: false,
      uploadImgUrl: "",
      newImages: [],
      imgFileList: [],
      ruleForm: {
        id: "",
        remark: "",
        img: "",
      },
      orderInfo: {},
      baopeiZhanghu: {},
      sellerZhanghu: "",
      rules: {},
      userType: "",
    };
  },
  created() {
    this.uploadImgUrl = process.env.VUE_APP_BASE_API + "/upload/image";
  },
  methods: {
    open(billInfo) {
      this.loading = false;
      this.orderInfo = billInfo;
      this.newImages = [];
      this.imgFileList = [];
      this.ruleForm.remark = "";
      this.ruleForm.img = "";
      this.ruleForm.id = this.orderInfo.id;
      this.dialogVisible = true;
    },
    // 拒绝打款
    refuse(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          // 拼接新上传的图片
          if (this.newImages && this.newImages.length) {
            let newArr = [];
            this.newImages.forEach((img) => {
              newArr.push(img.response.data.filePath);
            });
            this.ruleForm.img = newArr.join(",");
          } else {
            this.ruleForm.img = "";
          }
          payApi
            .tenantFinancialStatementRefuse(this.ruleForm)
            .then((res) => {
              this.dialogVisible = false;
              this.$message.success(res.msg);
              this.$parent.getList();
            })
            .catch((err) => {
              this.dialogVisible = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    submitForm(formName, flag) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let str = "确认已经打款完成了吗?";
          if (flag == 2) {
            str = "确认拒绝该笔打款申请吗?";
          }
          this.$confirm(str, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              this.loading = true;
              // 拼接新上传的图片
              if (this.newImages && this.newImages.length) {
                let newArr = [];
                this.newImages.forEach((img) => {
                  newArr.push(img.response.data.filePath);
                });
                this.ruleForm.img = newArr.join(",");
              } else {
                this.ruleForm.img = "";
              }
              if (flag == 2) {
                // 拒绝打款
                payApi
                  .tenantFinancialStatementRefuse(this.ruleForm)
                  .then((res) => {
                    this.dialogVisible = false;
                    this.$message.success(res.msg);
                    this.$parent.getList();
                  })
                  .catch((err) => {
                    this.dialogVisible = false;
                  });
              } else {
                // 完成打款
                payApi
                  .tenantFinancialStatementPayment(this.ruleForm)
                  .then((res) => {
                    this.dialogVisible = false;
                    this.$message.success(res.msg);
                    this.$parent.getList();
                  })
                  .catch((err) => {
                    this.dialogVisible = false;
                  });
              }
            })
            .catch(() => {});
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 凭证图片上传成功
    handleUploadSuccess(res, file, fileList) {
      if (res.code == 200) {
        // 取得新上传的文件
        this.newImages = fileList.filter((t) => {
          return t.url.indexOf("blob:") > -1;
        });
      }
    },
    // 凭证图片删除
    handleRemove(response, fileList, file) {
      const that = this;
      // 1.获取将要删除图片的临时路径
      const filePath = response.url;
      // 2.找到图片对应的索引值
      this.imgFileList.forEach(function (element, key) {
        if (element.url === filePath) {
          that.imgFileList.splice(key, 1); //回显的图片
        }
      });
      this.newImages.forEach(function (element, key) {
        if (element.url === filePath) {
          that.newImages.splice(key, 1); //新上传的图片
        }
      });
    },
    // 图片预览
    handlePictureCardPreview(file) {
      this.$viewerApi({
        images: [file.url],
      });
    },
    uploadNumber(file, fileList) {
      //限制图片数量
      this.$modal.msgError("最多只能上传5张图片");
    },
    close(formName) {
      this.dialogVisible = false;
    },
  },
};
</script>
<style scoped lang="scss">
::v-deep .el-dialog__body {
  padding-top: 10px !important;
}
::v-deep .el-upload-dragger {
  width: 148px;
  height: 148px;
}

.pick-dialog >>> .el-input--medium input {
  text-align: left;
}
</style>
